using Cysharp.Threading.Tasks;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Framework.Serialization;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public static class SkillViewUtil
    {
        public static ByteBuf Load(string path)
        {
            var task = GetSkillConfigOnMainThread(path);
            return task.AsTask().Result;
        }

        private static async UniTask<ByteBuf> GetSkillConfigOnMainThread(string path)
        {
            await UniTask.SwitchToMainThread();
#if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                var textAsset = UnityEditor.AssetDatabase.LoadAssetAtPath<TextAsset>("Assets/" + path);
                ByteBuf buff = new ByteBuf(textAsset.bytes);
                return buff;
            }
            else
#endif
            {
                var resloader = ResLoader.Create();
                var textAsset = resloader.LoadABAsset<TextAsset>(path);
                ByteBuf buff = new ByteBuf(textAsset.bytes);
                resloader.Put2Pool();
                return buff;
            }
        }
    }
}